* Directories
// cd /Users/xiaosongw/Dropbox/Research/InformedSources/Replication/Analysis
// local datdir /Users/xiaosongw/Dropbox/Research/InformedSources/Replication/Build/Output/

cd /Users/byrned/Dropbox/Research/InformedSources/Replication/Analysis/
local datdir /Users/byrned/Dropbox/Research/InformedSources/Replication/Build/Output/


* ----- Figure 7: Change in Margins Under Asymmetric Information Sharing ------*

* Load data
use `datdir'is_mel_p_2005_2019, clear

* Sample period
keep if t>=td(1may2015) & t<=td(31dec2017)

* day of week month of year dummies
gen moy = month(t)
gen dow = dow(t)

* Coles off the platform
gen coles_off=cond(t>td(15mar2016),1,0)

bysort id (t): gen dc=c-c[_n-1]
gen dc0=dc if st_cyc_day==1
replace dc0=0 if dc0==.

* create lagged cost changes
gen pos_dc=dc if dc>0 & ~missing(dc)
replace pos_dc=0 if dc<=0
gen neg_dc=dc-pos_dc
forval i=1/7 {
	bysort id (t): gen pos_dc`i'=pos_dc[_n-`i']
	bysort id (t): gen neg_dc`i'=neg_dc[_n-`i']
}

* normalize cycle length
drop st_cyc_day_norm
keep if st_cyc_len > 6
gen st_cyc_day_norm = 0 if st_cyc_day == 0
forval i = 1/10	{
	replace st_cyc_day_norm = `i' if (st_cyc_day >= ((st_cyc_len - 1) * (`i' - 1) / 10 + 1)) & 	///
								   (st_cyc_day <= ((st_cyc_len - 1) * `i' / 10 + 1))
}
* drop last cycle day 
replace st_cyc_day_norm = . if st_cyc_day == st_cyc_len - 1

* dummies for all Coles vs rivals, pre/post case, by cycle decile (paper)
local nday = 10
forval j=1/2{
	if(`j'==1){
		forval k=0/`nday'{
			gen pre_coles_`k'=(1-coles_off)*cond(bid==3 & st_cyc_day_norm==`k' & st_cyc_id>0,1,0)	/* Coles */
		}
		forval k=0/`nday'{
			gen post_coles_`k'=coles_off*cond(bid==3 & st_cyc_day_norm==`k' & st_cyc_id>0,1,0)	/* Coles */
		}
	}	
	if(`j'==2){
		forval k=0/`nday'{
			gen pre_rival_`k'=(1-coles_off)*cond(bid!=3 & st_cyc_day_norm==`k' & st_cyc_id>0,1,0)	/* Rivals */
		}	
		forval k=0/`nday'{
			gen post_rival_`k'=coles_off*cond(bid!=3 & st_cyc_day_norm==`k' & st_cyc_id>0,1,0)	/* Coles */
		}		
	}
}

* dummies for all retailers, pre/post case, by cycle decile (appendix) 
forval j=1/6{
	forval k=0/`nday'{
		gen pre_`j'_`k'=(1-coles_off)*cond(bid==`j' & st_cyc_day_norm==`k' & st_cyc_id>0,1,0)
	}	
	forval k=0/`nday'{
		gen post_`j'_`k'=coles_off*cond(bid==`j' & st_cyc_day_norm==`k' & st_cyc_id>0,1,0)
	}	
}
keep if bid>=1 & bid<=6


* save final data
sort id t
save Temp/regression_data, replace


* ----------------------- Margin Regression Coles vs Rivals -------------------*


* Load Data
use Temp/regression_data, clear

drop if t >= td(1mar2016) & t < td(1may2016)		/* cut out sample around the case */

* Melbourne (Coles vs Rivals)
reghdfe marg pre_coles_0-post_rival_10 pos_dc* neg_dc*, absorb(i.id i.dow i.moy) cluster(id t)

local nday = 10
tempname coef_plots
postfile `coef_plots' coef std lb ub bid cyc_day coles_off using Temp/mel_results_norm_dc, replace
forval j = 1/2 {
	if(`j'==1){
		forval i = 0/`nday' {
			qui lincom _cons +  pre_coles_`i'
			post `coef_plots' (r(estimate)) (r(se)) (r(lb)) (r(ub)) (`j') (`i') (0) 
			qui lincom _cons +  post_coles_`i'
			post `coef_plots' (r(estimate)) (r(se)) (r(lb)) (r(ub)) (`j') (`i') (1)
			qui lincom post_coles_`i' - pre_coles_`i'
			post `coef_plots' (r(estimate)) (r(se)) (r(lb)) (r(ub)) (`j') (`i') (-99)
		}		
	}
	if(`j'==2){
		forval i = 0/`nday' {
			qui lincom _cons +  pre_rival_`i'
			post `coef_plots' (r(estimate)) (r(se)) (r(lb)) (r(ub)) (`j') (`i') (0) 
			qui lincom _cons +  post_rival_`i'
			post `coef_plots' (r(estimate)) (r(se)) (r(lb)) (r(ub)) (`j') (`i') (1)
			qui lincom post_rival_`i' - pre_rival_`i'
			post `coef_plots' (r(estimate)) (r(se)) (r(lb)) (r(ub)) (`j') (`i') (-99)
		}		
	}	

}
postclose `coef_plots'


* ------------------ Margin Differences Coles vs Rivals -----------------------*

use Temp/mel_results_norm_dc, clear
replace cyc_day = cyc_day / 10

twoway 	(line coef cyc_day if coles_off==-99&bid==1, lcolor(gs0) lpattern(solid) lwidth(medthick)) ///
		(line lb cyc_day if coles_off==-99&bid==1, lcolor(gs0) lpattern(dash) lwidth(vthin)) ///
		(line ub cyc_day if coles_off==-99&bid==1, lcolor(gs0) lpattern(dash) lwidth(vthin)) ///
		(connected coef cyc_day if coles_off==-99&bid==2, lcolor(gs0*0.3) lpattern(solid) lwidth(medthick) msymbol(O) mcolor(gs0*0.3) msize(*1)) ///
		(line lb cyc_day if coles_off==-99&bid==2, lcolor(gs0*0.3) lpattern(dash) lwidth(vthin)) ///
		(line ub cyc_day if coles_off==-99&bid==2, lcolor(gs0*0.3) lpattern(dash) lwidth(vthin) ///		
		xtitle("Fraction of the Cycle Completed", height(0)) ytitle("Station-Level Margin Difference Estimate (cpl)") ///
		xlabel(0 `" "0" "Restoration" "' .5 "1/2" 1 `" "1" "Full" "Cycle" "',labsize(*0.8)) ylabel(0(2)8, grid) ///
		plotregion(margin(zero) style(none) fcolor(white)) graphregion(color(white)) bgcolor(white) ///
		legend(ring(0) size(*0.7) position(6) symx(*0.5) region(lwidth(*0.05)) rows(2) order(1 2 4 5) ///
		label(1 "Coles") label(2 "95% CI") label(4 "Other Major Retailers") label(5 "95% CI")))
graph export Output/fig6_margdiff.pdf, as(pdf) replace	

asf
* -------------- Testing Margin Differences Coles vs Rivals -------------------*

use Temp/regression_data, clear
drop if t >= td(15mar2016) & t <= td(15apr2016)		/* cut out sample around the case */
reghdfe marg pre_coles_0-post_rival_10 pos_dc* neg_dc*, absorb(i.id i.dow i.moy) cluster(id t)

* Pairwise tests of margin differences
forval i=0/10{
	test (post_coles_`i'-pre_coles_`i')=(post_rival_`i'-pre_rival_`i')
}

* Joint test of equality of profiles
test (post_coles_0-pre_coles_0) + ///
	 (post_coles_1-pre_coles_1) + ///
	 (post_coles_2-pre_coles_2) + ///
	 (post_coles_3-pre_coles_3) + ///
	 (post_coles_4-pre_coles_4) + ///
	 (post_coles_5-pre_coles_5) + ///
	 (post_coles_6-pre_coles_6) + ///
	 (post_coles_7-pre_coles_7) + ///
	 (post_coles_8-pre_coles_8) + ///
	 (post_coles_9-pre_coles_9) + ///
	 (post_coles_10-pre_coles_10) = ///
	 (post_rival_0-pre_rival_0) + ///
	 (post_rival_1-pre_rival_1) + ///
	 (post_rival_2-pre_rival_2) + ///
	 (post_rival_3-pre_rival_3) + ///
	 (post_rival_4-pre_rival_4) + ///
	 (post_rival_5-pre_rival_5) + ///
	 (post_rival_6-pre_rival_6) + ///
	 (post_rival_7-pre_rival_7) + ///
	 (post_rival_8-pre_rival_8) + ///
	 (post_rival_9-pre_rival_9) + ///
	 (post_rival_10-pre_rival_10)


	